package com.example.activitytest;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class FirstActivity extends BaseActivity {
    private static final String TAG = "FirstActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActivityResultLauncher<Intent> resultLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                o -> {
                    if (o.getResultCode() == RESULT_OK){
                        Intent intent = o.getData();
                        if(intent != null){
                            String str = intent.getStringExtra("data_return");
                            Log.d(TAG, "onActivityResult: DATA: "+ str );
                        }
                    }
                }
        );

        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout); //定义布局
        Log.d(TAG, "onCreate: Task id is " + getTaskId());
        //隐藏系统自带标题栏
        ActionBar actionbar = getSupportActionBar();
        if (actionbar != null){
            Log.d(TAG, "onCreate: action bar is not null");
            actionbar.hide();
        }

        Button toast1 = (Button) findViewById(R.id.button); //创建button对象
        //定义button点击后弹出toast
        toast1.setOnClickListener(view -> Toast.makeText(FirstActivity.this, "You clicked!", Toast.LENGTH_SHORT).show()
        );
        //关闭activity 按钮
        Button close_button = (Button) findViewById(R.id.button2);
        close_button.setOnClickListener(view -> finish());
        //换起另一个activity
        Button second_activity = (Button) findViewById(R.id.second_activity);
        second_activity.setOnClickListener(view -> {
            String data = "Hello Second Activity!";
            //Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            Intent intent = new Intent("com.example.activitytest.ACTION_START");
            //intent.addCategory("android.intent.category.DEFAULT");
            intent.putExtra("data_from_firstActivity",data);
            startActivity(intent);
        });

        //跳转至浏览器
        Button web_button = (Button) findViewById(R.id.web_button);
        web_button.setOnClickListener(view -> {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("http://www.baidu.com"));
            startActivity(Intent.createChooser(intent,"Choose Application"));
        });
        //拨打电话
        Button call_button = (Button) findViewById(R.id.call_button);
        call_button.setOnClickListener(view -> {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:10086"));
            startActivity(intent);
        });
        //下级向上级返回数据
        Button next_and_back = (Button) findViewById(R.id.button6);
        next_and_back.setOnClickListener(view -> {
            Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            intent.putExtra("data_from_firstActivity","Hello");
            resultLauncher.launch(intent);
        });

        //跳转到list view
        Button listView = (Button) findViewById(R.id.jump_to_list_view);
        listView.setOnClickListener(v -> {
            Intent intent = new Intent(FirstActivity.this,ListViewActivity.class);
            String[] data = {"123123","123144","aaaa","bbbbb","cccc","dddd","eeeee","ffff","ggggg","34243","gr34525"};
            intent.putExtra("string_array",data);
            startActivity(intent);
        });
    }

    //创建菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }
    //定义菜单item点击事件
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == R.id.add_item) {
            Toast.makeText(this, "You click add button", Toast.LENGTH_SHORT).show();
        } else if (itemId == R.id.remove_item) {
            Toast.makeText(this, "You click remove button", Toast.LENGTH_SHORT).show();
        }

        return true;
    }
}